<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="台账来源" prop="tzSource">
        <el-input
          v-model="queryParams.tzSource"
          placeholder="请输入台账来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运单ID" prop="jcShipmentCode">
        <el-input
          v-model="queryParams.jcShipmentCode"
          placeholder="请输入运单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['ledger:verification:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:verification:edit']"
        >核销</el-button>
      </el-col>
      <el-col :span="5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ledger:verification:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="5">
        <el-radio-group v-model="queryParams.hxStatus" style="margin-bottom: 30px;" size="mini" @input="getList">
          <el-radio-button v-for="dict in dict.type.HXZT" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
        </el-radio-group>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="verificationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="组织机构" align="center" prop="jcOrganizationName" />
      <el-table-column label="运单/订单/派车单" align="center" prop="jcShipmentCode">
        <template #default="scope">
          <span v-if="scope.row.jcOrderId">{{ scope.row.jcOrderCode }}</span>
          <span v-else-if="scope.row.jcShipmentId">({{ scope.row.jcShipmentCode }})</span>
          <span v-else>({{ scope.row.jcDispatchCode }})</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <span v-if="scope.row.type == '0'">收入</span>
          <span v-else>成本</span>
        </template>
      </el-table-column>
      <el-table-column label="台账来源" align="center" prop="tzSource" />
      <el-table-column label="总金额(元)" align="center" prop="zmoney" />
      <el-table-column label="已核销金额(元)" align="center" prop="hxMoney" />
      <el-table-column label="未核销金额(元)" align="center" prop="whxMoney" />
      <el-table-column label="核销次数" align="center" prop="hxCount" />
      <el-table-column label="核销状态" align="center" prop="hxStatus">
        <template #default="scope">
          <dict-tag :options="dict.type.HXZT" :value="scope.row.hxStatus" />
        </template>
      </el-table-column>
      <el-table-column label="台账明细" align="center">
        <template #default="scope">
          <el-link type="primary" :underline="false" @click="handleLedgeDetail(scope.row)">查看</el-link>
        </template>
      </el-table-column>
      <el-table-column label="总税金(元)" align="center" prop="zinput" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:verification:edit']"
          >核销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改核销记录对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="recordForm" :model="recordForm" :rules="rules" label-width="100px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="总金额" prop="zmoney">
              <el-input v-model="recordForm.zmoney" placeholder="请输入总金额" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="未核销金额" prop="whxMoney">
              <el-input v-model="recordForm.whxMoney" placeholder="请输入未核销金额" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已核销金额" prop="yhxMoney">
              <el-input v-model="recordForm.yhxMoney" placeholder="请输入已核销金额" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核销金额" prop="hxMoney">
              <el-input-number v-model="recordForm.hxMoney" placeholder="请输入核销金额" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="description">
              <el-input type="textarea" v-model="recordForm.description" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="10" class="mb8" v-if="recordForm.whxMoney !== 0 && recordForm.hxStatus !== 1">
        <div class="dialog-footer" style="text-align: center;">
          <el-button type="primary" @click="submitRecordForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-row>
      <el-divider content-position="center">核销记录详情信息</el-divider>
      <el-table v-loading="loading" :data="records">
        <el-table-column type="selection" width="55" align="center" fixed/>
        <el-table-column label="总金额" align="center" prop="zmoney" />
        <el-table-column label="已核销金额" align="center" prop="yhxMoney" />
        <el-table-column label="核销金额" align="center" prop="hxMoney" />
        <el-table-column label="核销人" align="center" prop="operator" />
        <el-table-column label="未核销金额" align="center" prop="whxMoney" />
        <el-table-column label="核销时间" align="center" prop="hxTime" width="180" />
        <el-table-column label="核销类型" align="center" prop="hxType" />
      </el-table>
    </el-dialog>

    <!-- 台账明细对话框 -->
    <el-dialog :close-on-click-modal="false" :title="ledgerDetailTitle" :visible.sync="ledgerDetailOpen" width="900px" append-to-body>
      <el-table :data="ledgerDetailList">
        <el-table-column label="费用类型" align="center" prop="cost">
          <template #default="scope">
            <dict-tag :options="dict.type.LedgerCost" :value="scope.row.cost" />
          </template>
        </el-table-column>
        <el-table-column label="费用名称" align="center" prop="jcFeeTypeName" />
        <el-table-column label="费用金额" align="center" prop="amount" />
        <el-table-column label="费用支出" align="center" prop="income" />
        <el-table-column label="费用税率" align="center" prop="taxrate" />
        <el-table-column label="费用税金" align="center" prop="input" />
        <el-table-column label="备注" align="center" prop="description" />
      </el-table>
      <div class="dialog-footer" style="text-align: center;margin-top: 20px;">
        <el-button type="primary" @click="closeLedgerDetail">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCostVerification,
  getVerification,
  addVerificationRecord
} from "@/api/ledger/verification";
import {orderFeeList} from "@/api/ledger/detail";

export default {
  name: "CostVerify",
  dicts: ["HXZT", "LedgerCost"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedJcVerificationRecord: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 核销记录表格数据
      verificationList: [],
      // 核销记录详情表格数据
      records: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 已核销金额时间范围
      daterangeCreateTime: [],
      // 已核销金额时间范围
      daterangeModifyTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createName: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        hxCount: null,
        hxMoney: null,
        whxMoney: null,
        hxStatus: '0',
        type: null,
        tzSource: null,
        zinput: null,
        zmoney: null,
        jcOrderId: null,
        jcOtherFeeId: null,
        jcShipmentId: null,
        jcDispatchId: null,
        jcOrganizationId: null
      },
      // 表单参数
      recordForm: {},
      // 表单校验
      rules: {
        hxMoney: [
          { required: true, message: "请输入核销金额", trigger: "blur" }
        ],
        jcVerificationzbId: [
          { required: true, message: "请输入核销主表ID", trigger: "blur" }
        ]
      },
      // 记录表格数据
      ledgerDetailList: [],
      ledgerDetailOpen: false,
      ledgerDetailTitle: "台账明细"
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询核销记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeModifyTime && '' != this.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.daterangeModifyTime[1];
      }
      listCostVerification(this.queryParams).then(response => {
        this.verificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.recordForm = {
        hxCount: null,
        hxMoney: null,
        whxMoney: null,
        hxStatus: null,
        type: null,
        tzSource: null,
        zinput: null,
        zmoney: null
      };
      this.records = [];
      this.resetForm("recordForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeModifyTime = [];
      this.resetForm("queryForm");
      this.queryParams.hxStatus = '0';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加核销记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.getInfo(id)
      this.open = true;
      this.title = "修改核销记录";
    },
    getInfo(id) {
      getVerification(id).then(response => {
        this.recordForm = response.data;
        this.recordForm.yhxMoney = response.data.hxMoney;
        this.recordForm.jcVerificationzbId = response.data.id;
        this.recordForm.id = null;
        this.recordForm.hxMoney = 0;
        this.records = response.data.records;
      });
    },
    /** 提交按钮 */
    submitRecordForm() {
      this.$refs["recordForm"].validate(valid => {
        if (this.recordForm.hxMoney <= 0) {
          this.$modal.msgError("核销金额必须大于0");
          return false;
        }
        if (this.recordForm.hxMoney > this.recordForm.whxMoney) {
          this.$modal.msgError("核销金额不能大于未核销金额");
          return false;
        }

        if (valid) {
          addVerificationRecord(this.recordForm).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.recordForm.hxMoney = 0;
            this.getInfo(this.recordForm.jcVerificationzbId);
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ledger/verification/export', {
        ...this.queryParams
      }, `verification_${new Date().getTime()}.xlsx`)
    },
    handleLedgeDetail(row) {
      this.ledgerDetailList = [];
      orderFeeList(row.jcShipmentId).then(response => {
        this.ledgerDetailList = response.data;
        this.ledgerDetailOpen = true;
      });
    },
    // 关闭台账明细弹出层
    closeLedgerDetail() {
      this.ledgerDetailOpen = false;
    }
  }
};
</script>
